<?php
declare(strict_types=1);
namespace App\Service\SnowFlake;
/**
 * @author chenshubo
 * 重写 Meta 生成器 
 */

use Hyperf\Snowflake\IdGenerator;

class SnowFlake 
{
    protected $idGenerator;
    public function __construct(IdGenerator\SnowflakeIdGenerator $idGenerator) 
    {
        $this->idGenerator = $idGenerator;
    }
    
    /**
     * 获取唯一主键ID
     */
    public function getPrimaryKeyId()
    {
        $workerId = config('snowflake.workerId');
        $dataCenterId = config('snowflake.dataCenterId');
        $meta = $this->idGenerator->getMetaGenerator()->generate();
        $meta->setWorkerId($workerId);
        $meta->setDataCenterId($dataCenterId);
        return $this->idGenerator->generate($meta);
    }
    
    /**
     * 解析唯一主键ID
     * @param int $id
     */
    public function explainPrimaryKeyId(int $id)
    {
        return $this->idGenerator->degenerate($id);
    }
}